home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / misc_lib / miscattr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  9.5 KB  |  334 lines

  1. /*****************************************************************************
  2. * Setting attributes for objects.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. *****************************************************************************/
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "irit_sm.h"
  11. #include "imalloc.h"
  12. #include "miscattr.h"
  13.  
  14. /*****************************************************************************
  15. *   Routine to set an integer attribute.                     *
  16. *****************************************************************************/
  17. void AttrSetIntAttrib(IPAttributeStruct **Attrs, char *Name, int Data)
  18. {
  19.     IPAttributeStruct
  20.         *Attr = AttrFindAttribute(*Attrs, Name);
  21.     
  22.     if (Attr) {
  23.     _AttrFreeAttributeData(Attr);
  24.     Attr -> U.I = Data;
  25.     Attr -> Type = IP_ATTR_INT;
  26.     }
  27.     else {
  28.     Attr = _AttrMallocAttribute(Name, IP_ATTR_INT);
  29.     Attr -> U.I = Data;
  30.     Attr -> Pnext = *Attrs;
  31.     *Attrs = Attr;
  32.     }
  33. }
  34.  
  35. /*****************************************************************************
  36. *   Routine to get an integer attribute.                     *
  37. *****************************************************************************/
  38. int AttrGetIntAttrib(IPAttributeStruct *Attrs, char *Name)
  39. {
  40.     IPAttributeStruct
  41.     *Attr = AttrFindAttribute(Attrs, Name);
  42.  
  43.     if (Attr != NULL) {
  44.     if (Attr -> Type == IP_ATTR_INT)
  45.         return Attr -> U.I;
  46.     else if (Attr -> Type == IP_ATTR_REAL)
  47.         return (int) Attr -> U.R;
  48.     else if (Attr -> Type == IP_ATTR_STR)
  49.         return atoi(Attr -> U.Str);
  50.     }
  51.  
  52.     return IP_ATTR_BAD_INT;
  53. }
  54.  
  55. /*****************************************************************************
  56. *   Routine to set a pointer attribute.                         *
  57. *****************************************************************************/
  58. void AttrSetPtrAttrib(IPAttributeStruct **Attrs, char *Name, VoidPtr Data)
  59. {
  60.     IPAttributeStruct
  61.         *Attr = AttrFindAttribute(*Attrs, Name);
  62.  
  63.     if (Attr) {
  64.     _AttrFreeAttributeData(Attr);
  65.     Attr -> U.Ptr = Data;
  66.     Attr -> Type = IP_ATTR_PTR;
  67.     }
  68.     else {
  69.     Attr = _AttrMallocAttribute(Name, IP_ATTR_PTR);
  70.     Attr -> U.Ptr = Data;
  71.     Attr -> Pnext = *Attrs;
  72.     *Attrs = Attr;
  73.     }
  74. }
  75.  
  76. /*****************************************************************************
  77. *   Routine to get a pointer attribute.                         *
  78. *****************************************************************************/
  79. VoidPtr AttrGetPtrAttrib(IPAttributeStruct *Attrs, char *Name)
  80. {
  81.     IPAttributeStruct
  82.     *Attr = AttrFindAttribute(Attrs, Name);
  83.  
  84.     if (Attr != NULL && Attr -> Type == IP_ATTR_PTR)
  85.         return Attr -> U.Ptr;
  86.     else
  87.         return NULL;
  88. }
  89.  
  90. /*****************************************************************************
  91. *   Routine to set a real attribute.                         *
  92. *****************************************************************************/
  93. void AttrSetRealAttrib(IPAttributeStruct **Attrs, char *Name, RealType Data)
  94. {
  95.     IPAttributeStruct
  96.         *Attr = AttrFindAttribute(*Attrs, Name);
  97.     
  98.     if (Attr) {
  99.     _AttrFreeAttributeData(Attr);
  100.     Attr -> U.R = Data;
  101.     Attr -> Type = IP_ATTR_REAL;
  102.     }
  103.     else {
  104.     Attr = _AttrMallocAttribute(Name, IP_ATTR_REAL);
  105.     Attr -> U.R = Data;
  106.     Attr -> Pnext = *Attrs;
  107.     *Attrs = Attr;
  108.     }
  109. }
  110.  
  111. /*****************************************************************************
  112. *   Routine to get a real attribute from an object.                 *
  113. *****************************************************************************/
  114. RealType AttrGetRealAttrib(IPAttributeStruct *Attrs, char *Name)
  115. {
  116.     IPAttributeStruct
  117.     *Attr = AttrFindAttribute(Attrs, Name);
  118.  
  119.     if (Attr != NULL) {
  120.     if (Attr -> Type == IP_ATTR_REAL)
  121.         return Attr -> U.R;
  122.     else if (Attr -> Type == IP_ATTR_INT)
  123.         return (RealType) Attr -> U.I;
  124.     else if (Attr -> Type == IP_ATTR_STR)
  125.     {
  126.         RealType r;
  127.  
  128.         sscanf("%lf", Attr -> U.Str, &r);
  129.         return r;
  130.     }
  131.     }
  132.  
  133.     return IP_ATTR_BAD_REAL * 10;
  134. }
  135.  
  136. /*****************************************************************************
  137. *   Routine to set a string attribute.                         *
  138. *****************************************************************************/
  139. void AttrSetStrAttrib(IPAttributeStruct **Attrs, char *Name, char *Data)
  140. {
  141.     IPAttributeStruct
  142.         *Attr = AttrFindAttribute(*Attrs, Name);
  143.  
  144.     if (Attr) {
  145.     _AttrFreeAttributeData(Attr);
  146.     Attr -> U.Str = IritStrdup(Data);
  147.     Attr -> Type = IP_ATTR_STR;
  148.     }
  149.     else {
  150.     Attr = _AttrMallocAttribute(Name, IP_ATTR_STR);
  151.     Attr -> U.Str = IritStrdup(Data);
  152.     Attr -> Pnext = *Attrs;
  153.     *Attrs = Attr;
  154.     }
  155. }
  156.  
  157. /*****************************************************************************
  158. *   Routine to get a string attribute.                         *
  159. *****************************************************************************/
  160. char *AttrGetStrAttrib(IPAttributeStruct *Attrs, char *Name)
  161. {
  162.     IPAttributeStruct
  163.     *Attr = AttrFindAttribute(Attrs, Name);
  164.  
  165.     if (Attr != NULL && Attr -> Type == IP_ATTR_STR)
  166.         return Attr -> U.Str;
  167.     else
  168.         return NULL;
  169. }
  170.  
  171. /*****************************************************************************
  172. *   Routine to scan a list of attributes. If TraceAttrs != NULL, a ptr to    *
  173. * its attribute list is saved and the next attribute is returned every call  *
  174. * until the end of the list is reached, in which NULL is returned.         *
  175. *   TraceAttrs should be NULL in all but the first call in the sequence.     *
  176. *   Attributes with names starting with an underscore '_' are assumed to be  *
  177. * temporary or internal and are skipped.                     * 
  178. *****************************************************************************/
  179. IPAttributeStruct *AttrTraceAttributes(IPAttributeStruct *TraceAttrs)
  180. {
  181.     static IPAttributeStruct
  182.     *Attrs = NULL;
  183.  
  184.     if (TraceAttrs != NULL)
  185.     Attrs = TraceAttrs;
  186.  
  187.     while (Attrs) {
  188.     if (Attrs -> Name[0] != '_') {
  189.         IPAttributeStruct
  190.             *RetVal = Attrs;
  191.  
  192.         Attrs = Attrs -> Pnext;
  193.         return RetVal;
  194.     }
  195.     else
  196.         Attrs = Attrs -> Pnext;
  197.     }
  198.  
  199.     return NULL;
  200. }
  201.  
  202. /*****************************************************************************
  203. *   Routine to convert an attribute to a string.                 *
  204. *****************************************************************************/
  205. char *Attr2String(IPAttributeStruct *Attr)
  206. {
  207.     static char Str[LINE_LEN_LONG];
  208.  
  209.     Str[0] = 0;
  210.  
  211.     switch (Attr -> Type) {
  212.     case IP_ATTR_INT:
  213.         sprintf(Str, "[%s %d]", Attr -> Name, Attr -> U.I);
  214.         break;
  215.     case IP_ATTR_REAL:
  216.         sprintf(Str, "[%s %lg]", Attr -> Name, Attr -> U.R);
  217.         break;
  218.     case IP_ATTR_STR:
  219.         if (strchr(Attr -> U.Str, '"') != NULL) {
  220.         /* Need to escape the quotes. */
  221.             int i, j;
  222.  
  223.         sprintf(Str, "[%s \"", Attr -> Name);
  224.  
  225.         /* Need to quote the string or escape the internal " */
  226.         for (i = j = 0; i < strlen(Attr -> U.Str); i++) {
  227.             if (Attr -> U.Str[i] == '"')
  228.             Str[j++] = '\\';
  229.             Str[j++] = Attr -> U.Str[i];
  230.         }
  231.         Str[j] = 0;
  232.         strcat(Str, "\"]");
  233.         }
  234.         else
  235.             sprintf(Str, "[%s \"%s\"]", Attr -> Name, Attr -> U.Str);
  236.         break;
  237.     case IP_ATTR_OBJ:
  238.     case IP_ATTR_PTR:
  239.         break;
  240.     default:
  241.         IritFatalError("Undefined attribute type");
  242.         break;
  243.     }
  244.  
  245.     return Str;
  246. }
  247.  
  248. /*****************************************************************************
  249. *   Routine to initialize the Attributes of the given Attr list.         *
  250. *****************************************************************************/
  251. void AttrResetAttributes(IPAttributeStruct **Attrs)
  252. {
  253.     *Attrs = NULL;
  254. }
  255.  
  256. /*****************************************************************************
  257. *   Routine to search for an attribute by name.                     *
  258. *****************************************************************************/
  259. IPAttributeStruct *AttrFindAttribute(IPAttributeStruct *Attrs, char *Name)
  260. {
  261.     for (; Attrs != NULL; Attrs = Attrs -> Pnext)
  262.     if (stricmp(Name, Attrs -> Name) == 0)
  263.         return Attrs;
  264.  
  265.     return NULL;
  266. }
  267.  
  268. /*****************************************************************************
  269. *   Routine to set the color of an object.                     *
  270. *****************************************************************************/
  271. IPAttributeStruct *_AttrMallocAttribute(char *Name, IPAttributeType Type)
  272. {
  273.     IPAttributeStruct
  274.     *Attr = (IPAttributeStruct *) IritMalloc(sizeof(IPAttributeStruct));
  275.  
  276.     Attr -> Type = Type;
  277.     Attr -> Name = IritStrdup(Name);
  278.     Attr -> Pnext = NULL;
  279.  
  280.     return Attr;
  281. }
  282.  
  283. /*****************************************************************************
  284. *   Routine to release attribute named Name from the given Attr list.         *
  285. *****************************************************************************/
  286. void AttrFreeOneAttribute(IPAttributeStruct **Attrs, char *Name)
  287. {
  288.     IPAttributeStruct *TmpAttr,
  289.     *Attr = *Attrs;
  290.  
  291.     if (Attr) {
  292.     if (stricmp(Name, Attr -> Name) == 0) {
  293.         /* It is the first one - delete first in list. */
  294.         *Attrs = Attr -> Pnext;
  295.         Attr -> Pnext = NULL;
  296.         AttrFreeAttributes(&Attr);
  297.     }
  298.     else {
  299.         /* Search the rest of the list and delete if found. */
  300.         while (Attr -> Pnext != NULL) {
  301.         if (stricmp(Name, Attr -> Pnext -> Name) == 0) {
  302.             TmpAttr = Attr -> Pnext;
  303.             Attr -> Pnext = TmpAttr -> Pnext;
  304.             TmpAttr -> Pnext = NULL;
  305.             AttrFreeAttributes(&TmpAttr);
  306.         }
  307.         else
  308.             Attr = Attr -> Pnext;
  309.         }
  310.     }
  311.     }
  312. }
  313.  
  314. /*****************************************************************************
  315. *   Routine to release all attributes of the given Attr list.             *
  316. *****************************************************************************/
  317. void AttrFreeAttributes(IPAttributeStruct **Attrs)
  318. {
  319.     IPAttributeStruct
  320.     *Attr = *Attrs;
  321.  
  322.     while (Attr) {
  323.         IPAttributeStruct
  324.         *Next = Attr -> Pnext;
  325.  
  326.     _AttrFreeAttributeData(Attr);
  327.     IritFree((VoidPtr) Attr -> Name);
  328.     IritFree((VoidPtr) Attr);
  329.     Attr = Next;
  330.     }
  331.  
  332.     *Attrs = NULL;
  333. }
  334.